package be.kuleuven.mume.beans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import be.kuleuven.mume.PMF;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class VakBean {
	
	//@NotPersistent
	//private static final Logger log = Logger.getLogger(VakBean.class.getName());
	//@NotPersistent
	//private static final PersistenceManager pm = PMF.get().getPersistenceManager();
	//private static final PersistenceManager pm = PMF.getPersistenceManager();
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key vakId;
	@Persistent
	private String name;
	@Persistent
	private String hashTag;
	@Persistent
	private Key prof;
	@Persistent
	private List<String> studenten;
	
	public VakBean() {
		name = "";
		hashTag = "";
		studenten = new ArrayList<String>();
	}
	
	public void setVakId(String key) {
		this.vakId = KeyFactory.stringToKey(key);
	}

	public String getVakId() {
		return KeyFactory.keyToString(vakId);
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setHashTag(String hashTag) {
		this.hashTag = hashTag;
	}

	public String getHashTag() {
		return hashTag;
	}
	
	public ProfBean getProf() {
		PersistenceManager pm = PMF.getPersistenceManager();
		ProfBean prof = pm.getObjectById(ProfBean.class, this.prof);
		pm.close();
		return prof;
	}
	
	public void store() {
		PersistenceManager pm = PMF.getPersistenceManager();
		pm.makePersistent(this);
	}
	
	public static void delete(String vakId) {
		PersistenceManager pm = PMF.getPersistenceManager();
		Key key = KeyFactory.stringToKey(vakId);
		VakBean vak = pm.getObjectById(VakBean.class, key);
		pm.deletePersistent(vak);
	}

	public void setProf(String profId) {
		this.prof =  KeyFactory.stringToKey(profId);
	}
	
	public void setStudenten(List<String> studenten) {
		this.studenten = studenten;
	}

	//Add boolean argument to disable studenten list to JSON output
	//public List<String> getStudenten(boolean dummy) {
	public List<String> getStudenten() {
		return Collections.unmodifiableList(studenten);
	}

	public boolean addStudent(String persoonId) throws IllegalArgumentException{
		if(studenten.contains(persoonId))
			throw new IllegalArgumentException("Vak.studenten has already persoon:" + persoonId);
		studenten.add(persoonId);
		return true;
	}

	public Boolean removeStudent(String persoonId) throws IllegalArgumentException {
		if(this.studenten.remove(persoonId))
			return true;
		else
			throw new IllegalArgumentException("Vak.Studenten no persoon found for:" + persoonId);
	}
}
